package com.aifast.crm.customer.dao;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

import com.aifast.common.base.BaseEntity;
import com.aifast.common.dataScope.DataScope;
import com.aifast.crm.customer.entity.CrmCustomer;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 客户信息Mapper接口
 *
 * @author fujian
 * @date 2022-08-24
 */
@Mapper
public interface CrmCustomerDao extends BaseMapper<CrmCustomer> {

    default LambdaQueryWrapper<CrmCustomer> select(CrmCustomer pm) {
        return new LambdaQueryWrapper<CrmCustomer>()
                .eq(ObjectUtil.isNotNull(pm.getSex()), CrmCustomer::getSex, pm.getSex())
                .eq(ObjectUtil.isNotNull(pm.getType()), CrmCustomer::getType, pm.getType())
                .eq(ObjectUtil.isNotNull(pm.getSource()), CrmCustomer::getSource, pm.getSource())
                .eq(ObjectUtil.isNotNull(pm.getLabel()), CrmCustomer::getLabel, pm.getLabel())
                .eq(StrUtil.isNotBlank(pm.getIsJoinWx()), CrmCustomer::getIsJoinWx, pm.getIsJoinWx())
                .between(StrUtil.isNotBlank(pm.getCreateTime()), BaseEntity::getCreateTime, pm.getBeginTime(pm.getCreateTime()), pm.getEndTime(pm.getCreateTime()))
                .apply(StrUtil.isNotBlank(pm.getArea()), "(find_in_set({0},area))", pm.getArea())
                .orderByDesc(CrmCustomer::getId);
    }

    @DataScope
    List<CrmCustomer> list(CrmCustomer pm);

}
